package com.inool.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inool.common.result.Result;
import com.inool.model.Teacher;
import com.inool.service.TeacherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "教师管理")
@RestController
@RequestMapping("/api/teachers")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @Operation(summary = "添加教师")
    @PostMapping("/add")
    public Result<Teacher> addTeacher(@RequestBody Teacher teacher) {
        Teacher newTeacher = teacherService.addTeacher(teacher);
        return Result.ok(newTeacher);
    }

    @Operation(summary = "修改教师信息")
    @PostMapping("/update/{id}")
    public Result<Teacher> updateTeacher(@PathVariable String id, @RequestBody Teacher teacher) {
        Teacher updatedTeacher = teacherService.updateTeacherInfo(id, teacher);
        if (updatedTeacher != null) {
            return Result.ok(updatedTeacher);
        }
        return Result.ok();
    }

    @Operation(summary = "删除教师")
    @PostMapping("/remove/{id}")
    public Result<Void> deleteTeacher(@PathVariable String id) {
        teacherService.removeTeacher(id);
        return Result.ok();
    }

    @Operation(summary = "获取教师信息")
    @GetMapping("/{id}")
    public Result<Teacher> getTeacherById(@PathVariable String id) {
        Teacher teacher = teacherService.getTeacherById(id);
        if (teacher != null) {
            return Result.ok(teacher);
        }
        return Result.ok();
    }

    @Operation(summary = "获取所有教师信息")
    @GetMapping
    public Result<List<Teacher>> getAllTeachers() {
        List<Teacher> teachers = teacherService.getAllTeachers();
        return Result.ok(teachers);
    }

    @Operation(summary = "分页查询教师")
    @GetMapping("page")
    public Result<Page<Teacher>> pageTeachers(@RequestParam long current, @RequestParam long size,
                                              String staffNumber, String name, String title) {
        Page<Teacher> page = teacherService.pageTeachers(current, size, staffNumber, name, title);
        return Result.ok(page);
    }

}    